package com.iris.live.services.report.sources;

import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.DataCheckRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/23.
 */
@Component("ExportDataCheckScFalseRateReportSource")
public class ExportDataCheckScFalseRateReportSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportDataCheckScFalseRateReportSource.class);

    @Autowired
    private DataCheckRepository dataCheckRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商_数据核验_错误率报告_销售顾问", "经销商_数据核验_错误率报告_销售顾问", true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        List<Row> values = new ArrayList<>();

        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String startDate = context.get("exportStartDate") == null ? null : context.get("exportStartDate").toString();
        String endDate = context.get("exportEndDate") == null ? null : context.get("exportEndDate").toString();
        String nowDate = null;
        if (StringUtils.isNullOrBlank(startDate) && StringUtils.isNullOrBlank(endDate)) {
            nowDate = DateFormatUtils.getFormatDateToString(new java.util.Date(), "yyyyMMdd");
        }
        List list = dataCheckRepository.getScFalseRate(permit, nowDate, startDate, endDate);

        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());

        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map<String, Object> map = (Map) o;
                values.add(new Row(getCells(map)));
            }
        }
        return values;
    }

    private Cell[] getCells (Map<String, Object> map) {
        Cell[] cells = new Cell[30];
        int index = 0;
        cells[index++] = new Cell(map.get("sc") == null ? "" : map.get("sc").toString(), index);
        cells[index++] = new Cell(map.get("订单日期") == null ? "" : map.get("订单日期").toString(), index);
        cells[index++] = new Cell(map.get("订单人姓名") == null ? "" : map.get("订单人姓名").toString(), index);
        cells[index++] = new Cell(map.get("订金金额") == null ? "" : map.get("订金金额").toString(), index);
        cells[index++] = new Cell(map.get("合同签约日期") == null ? "" : map.get("合同签约日期").toString(), index);
        cells[index++] = new Cell(map.get("开票日期") == null ? "" : map.get("开票日期").toString(), index);
        cells[index++] = new Cell(map.get("开票人姓名") == null ? "" : map.get("开票人姓名").toString(), index);
        cells[index++] = new Cell(map.get("交车日期") == null ? "" : map.get("交车日期").toString(), index);
        cells[index++] = new Cell(map.get("车辆销售价（含税）") == null ? "" : map.get("车辆销售价（含税）").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车_保险保费金额") == null ? "" : map.get("经销商随车_保险保费金额").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车_贴息金额") == null ? "" : map.get("经销商随车_贴息金额").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车_A保次数") == null ? "" : map.get("经销商随车_A保次数").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车_B保次数") == null ? "" : map.get("经销商随车_B保次数").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车_尚银卡次数") == null ? "" : map.get("经销商随车_尚银卡次数").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车_逸蓝卡次数") == null ? "" : map.get("经销商随车_逸蓝卡次数").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车_购物卡价值") == null ? "" : map.get("经销商随车_购物卡价值").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车_加油卡价值") == null ? "" : map.get("经销商随车_加油卡价值").toString(), index);
        cells[index++] = new Cell(map.get("经销商随车精品价值") == null ? "" : map.get("经销商随车精品价值").toString(), index);
        cells[index++] = new Cell(map.get("BMBS随车_保险") == null ? "" : map.get("BMBS随车_保险").toString(), index);
        cells[index++] = new Cell(map.get("BMBS随车_SSSC") == null ? "" : map.get("BMBS随车_SSSC").toString(), index);
        cells[index++] = new Cell(map.get("BMBS随车_SD卡") == null ? "" : map.get("BMBS随车_SD卡").toString(), index);
        cells[index++] = new Cell(map.get("BMBS随车_加油卡") == null ? "" : map.get("BMBS随车_加油卡").toString(), index);
        cells[index++] = new Cell(map.get("BMBS随车_实物礼品") == null ? "" : map.get("BMBS随车_实物礼品").toString(), index);
        cells[index++] = new Cell(map.get("BMBS随车_服务礼券") == null ? "" : map.get("BMBS随车_服务礼券").toString(), index);
        cells[index++] = new Cell(map.get("BMBS购置税补贴SI") == null ? "" : map.get("BMBS购置税补贴SI").toString(), index);
        cells[index++] = new Cell(map.get("精品销售额（含税）") == null ? "" : map.get("精品销售额（含税）").toString(), index);
        cells[index++] = new Cell(map.get("五星延保销售额（含税）") == null ? "" : map.get("五星延保销售额（含税）").toString(), index);
        cells[index++] = new Cell(map.get("上牌服务费") == null ? "" : map.get("上牌服务费").toString(), index);
        cells[index++] = new Cell(map.get("其他服务费") == null ? "" : map.get("其他服务费").toString(), index);
        cells[index++] = new Cell(map.get("金融服务费") == null ? "" : map.get("金融服务费").toString(), index);
        return cells;
    }
}
